package qlsl.androiddesign.view.subview.commonview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import qlsl.androiddesign.activity.commonactivity.MainActivity;
import qlsl.androiddesign.activity.subactivity.BannerCategoryActivity;
import qlsl.androiddesign.activity.subactivity.CategoryListActivity;
import qlsl.androiddesign.activity.subactivity.FirstCategoryActivity;
import qlsl.androiddesign.activity.subactivity.MessageCenterActivity;
import qlsl.androiddesign.activity.subactivity.SearchActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingCarActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingListActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingReleaseActivity;
import qlsl.androiddesign.adapter.baseadapter.BaseExpandableListAdapter;
import qlsl.androiddesign.adapter.commonadapter.ShoppingExpAdapter;
import qlsl.androiddesign.adapter.subadapter.ForumMenuAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.Category.RowsBean;
import qlsl.androiddesign.entity.otherentity.HomeBanner;
import qlsl.androiddesign.entity.otherentity.ShoopChildList;
import qlsl.androiddesign.entity.otherentity.ThreeCategory;
import qlsl.androiddesign.fragment.commonfragment.FragmentTab2;
import qlsl.androiddesign.http.service.commonservice.HomeService;
import qlsl.androiddesign.http.service.subservice.ShoppingService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshScrollView;
import qlsl.androiddesign.listener.OnPullRefreshListener;
import qlsl.androiddesign.method.UserMethod;
import qlsl.androiddesign.util.commonutil.DensityUtils;
import qlsl.androiddesign.util.commonutil.Log;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.commonview.ChildViewPager;
import qlsl.androiddesign.view.commonview.ChildViewPager.OnSingleTouchListener;
import qlsl.androiddesign.view.commonview.WrapExpandableListView;
import qlsl.androiddesign.view.indicatorview.AutoPlayManager;
import qlsl.androiddesign.view.indicatorview.ImageIndicatorView.OnItemClickListener;
import qlsl.androiddesign.view.indicatorview.IndicatorView;

public class TabView2 extends FunctionView<MainActivity> implements
		OnItemClickListener, OnSingleTouchListener {

	private FragmentTab2 fragment;

	private PullToRefreshScrollView refreshView;

	private IndicatorView indicatorView;

	private AutoPlayManager autoBrocastManager;

	private GridView gridView;

	private WrapExpandableListView listView;

	private List<RowsBean> list = new ArrayList<RowsBean>();

	private ArrayList<HashMap<String, RowsBean>> groupList = new ArrayList<HashMap<String, RowsBean>>();

	private ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>> childList = new ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>>();

	private Pager pager;

	public TabView2(FragmentTab2 fragment, ViewGroup rootView,
			ViewGroup contentView) {
		super((MainActivity) fragment.getActivity());
		this.fragment = fragment;
		setContentView(rootView, contentView);
	}

	public void onTabResume() {
		refreshView.getRefreshableView().smoothScrollTo(0, 0);
	}

	protected void initView(View view) {
		setTitle(activity.getResources().getString(R.string.tab2));
		setRightButtonResource(R.drawable.personal_h);
		showRightButton();
		if (UserMethod.isBusiness()) {
			addBorderView(R.drawable.forum_posth);
		}
		refreshView = findViewById(R.id.refreshScrollView);
		gridView = findViewById(R.id.gridView);
		listView = findViewById(R.id.expandableListView);
		indicatorView = findViewById(R.id.net_indicate_view);
		indicatorView.setIndicateStyle(IndicatorView.INDICATE_ROUND_STYLE);
		indicatorView.setupLayoutByDefaultResource(1);
		indicatorView.show();
	}

	/**
	 * 在标题栏右边按钮左边再加上一个图片控件
	 */
	public void addBorderView(int resid) {
		RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);

		lp2.addRule(RelativeLayout.CENTER_VERTICAL);
		lp2.setMargins(DensityUtils.dp2px(activity, 10), 0, 0, 0);

		View view_border = activity.getLayoutInflater().inflate(
				R.layout.imageview_border, null);
		view_border.setBackgroundResource(resid);
		view_border.setLayoutParams(lp2);

		ViewGroup titleView = (ViewGroup) view.findViewById(R.id.title_view);
		titleView.addView(view_border, titleView.getChildCount() - 1);
	}

	protected void initData() {
		setGridViewData();
		startHomeImageManager();
		HomeService.queryBannerList(this, fragment);
		ShoppingService.queryCatagoryList(this, fragment);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(onRefreshListener);
		indicatorView.setOnItemClickListener(this);
		ChildViewPager viewPager = findViewById(R.id.viewpager_image);
		viewPager.setIntercept(true);
		viewPager.setOnSingleTouchListener(this);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			ArrayList<HashMap<String, RowsBean>> groupListResult = (ArrayList<HashMap<String, RowsBean>>) map
					.get("groupList");
			ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>> childListResult = (ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>>) map
					.get("childList");
			pager = (Pager) map.get("pager");

			if (pager.getPageNo() == 1) {
				resetList(groupListResult, childListResult);
			} else {
				groupList.addAll(groupListResult);
				// childList.addAll(childListResult);
			}
			notifyDataSetChanged();
		} else if (t[0] instanceof List) {
			List<HomeBanner> list = (List<HomeBanner>) t[0];
			indicatorView.setTag(list);
			List<String> urls = new ArrayList<String>();
			for (HomeBanner item : list) {
				urls.add(item.getUrl());
			}
			indicatorView.setupLayout(urls);
			indicatorView.show();
		}
	}

	@SuppressWarnings("unchecked")
	public <T> void showDataChild(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>> childListResult = (ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>>) map
					.get("childList");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				childList.clear();
				childList.addAll(childListResult);
			}
			notifyDataSetChanged();
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.layout_child:
			doClickChildView(view);
			break;
		case R.id.layout_group:
			doClickGroupView(view);
			break;
		case R.id.grid_item:
			doClickGridItemView(view);
			break;
		case R.id.iv_border:
			doClickReleaseItemView(view);
			break;
		}

	}

	private void startHomeImageManager() {
		autoBrocastManager = new AutoPlayManager(indicatorView);
		autoBrocastManager.setBroadcastEnable(true);
		autoBrocastManager.setBroadCastTimes(Integer.MAX_VALUE);
		autoBrocastManager.loop();
	}

	private void setGridViewData() {
		Integer[] imgs = new Integer[] { R.drawable.classify_h,
				R.drawable.shopping_cart_h, R.drawable.search_h,
				R.drawable.car_home };
		String[] texts = new String[] { "分类", "购物车", "搜索", "消息" };
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int index = 0; index < imgs.length; index++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("icon", imgs[index]);
			map.put("text", texts[index]);
			list.add(map);
		}
		ForumMenuAdapter adapter = new ForumMenuAdapter(activity, list);
		gridView.setAdapter(adapter);
	}

	public void doClickReleaseItemView(View view) {
		startActivity(ShoppingReleaseActivity.class);
	}

	public void doClickGridItemView(View view) {
		int position = gridView.getPositionForView(view);
		if (position == 0) {// 分类
			startActivity(FirstCategoryActivity.class);
		} else if (position == 1) {// 购物车
			startActivity(ShoppingCarActivity.class);
		} else if (position == 2) {// 搜索
			startActivity(SearchActivity.class);
		} else if (position == 3) {// 消息
			startActivity(MessageCenterActivity.class);
		}
	}

	private void doClickGroupView(View view) {
		int position = listView.getPositionForView(view);
		long flatPostion = listView.getExpandableListPosition(position);
		int groupPosition = ExpandableListView
				.getPackedPositionGroup(flatPostion);
		RowsBean item = groupList.get(groupPosition).get("group");
		// if (item.getSupplierid().equals("16")) {
		OnOilItemClick(view, position, item);
		// } else {
		// int childSize = childList.get(groupPosition).size();
		// if (childSize == 0) {
		// ShoppingService.queryCatagoryListChild(listView,
		// childList.get(groupPosition), item.getSupplierid(), 0,
		// this, fragment);
		// } else {
		// showToast("已经有数据了");
		// }
		// }
	}

	@SuppressWarnings("unchecked")
	public void OnOilItemClick(View view, int position, RowsBean item) {
		// List<HomeBanner> list = (List<HomeBanner>) indicatorView.getTag();
		// if (list != null && list.size() > position) {
		// HomeBanner item = list.get(position);
		// startActivity(item, BannerCategoryActivity.class);

		ThreeCategory.RowsBean rowsBean = new ThreeCategory.RowsBean();
		rowsBean.setPhoto("");
		rowsBean.flage = 1;
		rowsBean.setSumcierid("" + item.getSupplierid());
		rowsBean.setSumciertype("" + item.getSuppliertype());
		startActivity(rowsBean, ShoppingListActivity.class);
		// }
	}

	private void doClickChildView(View view) {

		int position = listView.getPositionForView(view);
		long flatPostion = listView.getExpandableListPosition(position);
		int groupPosition = ExpandableListView
				.getPackedPositionGroup(flatPostion);
		int childPosition = ExpandableListView
				.getPackedPositionChild(flatPostion);
		ShoopChildList.RowsBean item = childList.get(groupPosition)
				.get(childPosition).get("child");
		if (item != null) {
			startActivity(item, CategoryListActivity.class);
		}

		// ShoppingService.queryCatagoryList(null, 0, this, fragment);
	}

	private void resetList(
			ArrayList<HashMap<String, RowsBean>> groupListResult,
			ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>> childListResult) {
		groupList.clear();
		groupList.addAll(groupListResult);
		childList.clear();
		childList.addAll(childListResult);
		refreshView.getRefreshableView().smoothScrollTo(0, 0);
	}

	public void notifyDataSetChanged() {
		BaseExpandableListAdapter adapter = (BaseExpandableListAdapter) listView
				.getExpandableListAdapter();
		if (adapter == null) {
			adapter = new ShoppingExpAdapter(activity, listView, groupList,
					childList);
			listView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		adapter.expandGroup();
		refreshView.setReleaseLabel(pager.getPageNo(), groupList.size(),
				pager.getTotalPage(), pager.getTotalCount());
		refreshView.onRefreshComplete();
	}

	private OnPullRefreshListener<ScrollView> onRefreshListener = new OnPullRefreshListener<ScrollView>() {

		public void onPullDown() {
			refreshView.onRefreshComplete();
			// queryComment(0);
			// ShoppingService.queryCatagoryList(this, fragment);
		}

		public void onPullUp() {
			super.onPullUp(refreshView, pager);
		}

		public void nextPager() {
			refreshView.onRefreshComplete();
			// queryComment(pager.getPageNo() + 1);
		}

	};

	@SuppressWarnings("unchecked")
	public void OnItemClick(View view, int position) {
		List<HomeBanner> list = (List<HomeBanner>) indicatorView.getTag();
		if (list != null && list.size() > position) {
			// HomeBanner item = list.get(position);
			// startActivity(item, BannerCategoryActivity.class);

			ThreeCategory.RowsBean rowsBean = new ThreeCategory.RowsBean();
			rowsBean.setPhoto("");
			rowsBean.setSumcierid("0");
			rowsBean.setSumciertype("");
			startActivity(rowsBean, ShoppingListActivity.class);
		}
	}

	public void onSingleTouch() {
		ChildViewPager viewPager = findViewById(R.id.viewpager_image);
		int position = viewPager.getCurrentItem();
		OnItemClick(null, position);
	}
}
